home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter4 / addpage.c next >
C/C++ Source or Header  |  1996-03-06  |  8KB  |  284 lines

  1.  
  2. #include <windows.h>  
  3. #include "addpage.h"  
  4. #include "commctrl.h"
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. HWND    hPropSheet  = NULL;
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "PropSheet_AddPage"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       if ( IsWindow( hPropSheet ) && PropSheet_IsDialogMessage( hPropSheet, &msg ) )
  75.          continue;
  76.  
  77.       TranslateMessage( &msg ); 
  78.       DispatchMessage( &msg );  
  79.    }
  80.  
  81.    return( msg.wParam ); 
  82. }
  83.  
  84.  
  85. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  86. {
  87.    WNDCLASSEX wcex;
  88.  
  89.    wcex.style         = lpwc->style;
  90.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  91.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  92.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  93.    wcex.hInstance     = lpwc->hInstance;
  94.    wcex.hIcon         = lpwc->hIcon;
  95.    wcex.hCursor       = lpwc->hCursor;
  96.    wcex.hbrBackground = lpwc->hbrBackground;
  97.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  98.    wcex.lpszClassName = lpwc->lpszClassName;
  99.  
  100.    // Added elements for Windows 95.
  101.    //...............................
  102.    wcex.cbSize = sizeof(WNDCLASSEX);
  103.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  104.                             IMAGE_ICON, 16, 16,
  105.                             LR_DEFAULTCOLOR );
  106.             
  107.    return RegisterClassEx( &wcex );
  108. }
  109.  
  110.  
  111.  
  112. LRESULT CALLBACK PageProc( HWND hDlg, UINT message,        
  113.                            WPARAM wParam, LPARAM lParam )
  114. {
  115. static NMHDR* nmhdr;
  116.  
  117.    switch (message) 
  118.    {
  119.        case WM_INITDIALOG: 
  120.                return (TRUE);
  121.  
  122.        case WM_NOTIFY :
  123.                nmhdr = (NMHDR*)lParam;
  124.                switch( nmhdr->code )
  125.                {
  126.                        // The user selected the Cancel button
  127.                        // and the properties that has been applied
  128.                        // needs to be restored.
  129.                        //.........................................
  130.                   case PSN_RESET :
  131.                          // Reset data back to original state.
  132.                          // ..................................
  133.                          break;
  134.  
  135.                        // Cancel or Close Pressed.
  136.                        //.........................
  137.                   case PSN_QUERYCANCEL :
  138.                          DestroyWindow( hPropSheet );
  139.                          break;
  140.  
  141.                        // OK Button Pressed.
  142.                        //...................
  143.                   case PSN_APPLY :
  144.                          DestroyWindow( hPropSheet );
  145.                          break;
  146.  
  147.                }
  148.                break;
  149.    }
  150.  
  151.    return( FALSE );
  152. }
  153.  
  154.  
  155. VOID DisplayProperties( HWND hWnd )
  156. {
  157.    PROPSHEETPAGE   psp;
  158.    PROPSHEETHEADER psh;
  159.    HPROPSHEETPAGE  hpsp;
  160.  
  161.    // Create the property pages.
  162.    //...........................
  163.    psp.dwSize    = sizeof( PROPSHEETPAGE );
  164.    psp.dwFlags   = PSP_DEFAULT;
  165.    psp.hInstance = hInst;
  166.    psp.pszTemplate = "InitialPage";
  167.    psp.pfnDlgProc  = (DLGPROC)PageProc;
  168.    hpsp = CreatePropertySheetPage( &psp );
  169.  
  170.    // Initialize the property sheet structure.
  171.    //.........................................
  172.    memset( &psh, 0, sizeof( PROPSHEETHEADER ) );
  173.    psh.dwSize     = sizeof( PROPSHEETHEADER );
  174.    psh.dwFlags    = PSH_USEICONID | PSH_MODELESS | PSH_NOAPPLYNOW;
  175.    psh.hInstance  = hInst;
  176.    psh.hwndParent = hWnd;
  177.    psh.pszIcon    = "SMALL";
  178.    psh.nPages     = 1;
  179.    psh.phpage     = &hpsp;
  180.    psh.pszCaption = "Properties";
  181.  
  182.    // Create a modeless property sheet.
  183.    //..................................
  184.    hPropSheet = (HWND)PropertySheet( &psh );
  185. }
  186.  
  187.  
  188. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  189. {
  190. static HPROPSHEETPAGE hPage = NULL;
  191.  
  192.    switch( uMsg )
  193.    {
  194.       case WM_CREATE :
  195.               InitCommonControls();
  196.               break;
  197.  
  198.       case WM_COMMAND :
  199.               switch( LOWORD( wParam ) )
  200.               {
  201.                  case IDM_TEST :
  202.                         if ( !IsWindow( hPropSheet ) )
  203.                            DisplayProperties( hWnd );
  204.                         break;
  205.  
  206.                  case IDM_ADDPAGE :
  207.                         if ( IsWindow( hPropSheet ) && !hPage )
  208.                         {
  209.                            PROPSHEETPAGE psp;
  210.  
  211.                            // Create a new page.
  212.                            //...................
  213.                            psp.dwSize    = sizeof( PROPSHEETPAGE );
  214.                            psp.dwFlags   = PSP_DEFAULT;
  215.                            psp.hInstance = hInst;
  216.                            psp.pszTemplate = "AddedPage";
  217.                            psp.pfnDlgProc  = (DLGPROC)PageProc;
  218.                            hPage = CreatePropertySheetPage( &psp );
  219.  
  220.                            // Add the page to the property sheet.
  221.                            //....................................
  222.                            PropSheet_AddPage( hPropSheet, hPage );
  223.                         }
  224.                         break; 
  225.  
  226.                  case IDM_REMPAGE :
  227.                         if ( IsWindow( hPropSheet ) && hPage )
  228.                         {
  229.                            // Remove the page to the property sheet.
  230.                            //.......................................
  231.                            PropSheet_RemovePage( hPropSheet, 1, hPage );
  232.                            hPage = NULL;
  233.                         }
  234.                         break;
  235.  
  236.                  case IDM_ABOUT :
  237.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  238.                         break;
  239.  
  240.                  case IDM_EXIT :
  241.                         DestroyWindow( hWnd );
  242.                         break;
  243.               }
  244.               break;
  245.       
  246.       case WM_DESTROY :
  247.               PostQuitMessage(0);
  248.  
  249.               if ( hPage )
  250.                  DestroyPropertySheetPage( hPage );
  251.  
  252.               break;
  253.  
  254.       default :
  255.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  256.    }
  257.  
  258.    return( 0L );
  259. }
  260.  
  261.  
  262. LRESULT CALLBACK About( HWND hDlg,           
  263.                         UINT message,        
  264.                         WPARAM wParam,       
  265.                         LPARAM lParam)
  266. {
  267.    switch (message) 
  268.    {
  269.        case WM_INITDIALOG: 
  270.                return (TRUE);
  271.  
  272.        case WM_COMMAND:                              
  273.                if (   LOWORD(wParam) == IDOK         
  274.                    || LOWORD(wParam) == IDCANCEL)    
  275.                {
  276.                        EndDialog(hDlg, TRUE);        
  277.                        return (TRUE);
  278.                }
  279.                break;
  280.    }
  281.  
  282.    return (FALSE); 
  283. }
  284.